var dictKeyArray = [];
layui.use(['element','table','form','layer','laydate'], function(){
    var table = layui.table,
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    layer = layui.layer;

    //日期
    laydate.render({
        elem: '#systemTime',
        trigger : 'click',
        type: 'datetime',
    });
    //执行一个laydate实例
    var beginDateInit = function(startId,endId ){
        laydate.render({
            elem: startId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date(value).getTime();
                var endTime = new Date($(endId).val()).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(startId).val('');
                }
            }
        })
    }
    var endDateInit = function(startId,endId ){
        laydate.render({
            elem: endId,
            type: 'datetime',
            done: function (value ) {
                var startDate = new Date($(startId).val()).getTime();
                var endTime = new Date(value).getTime();
                if (endTime < startDate) {
                    layer.msg('结束时间不能小于开始时间');
                    $(endId).val('');
                }
            }
        })
    }

    // 开门记录时间
    beginDateInit('#openBeginDate','#openEndDate');
    endDateInit('#openBeginDate','#openEndDate');

    $.get('/zxkRecord/loadTypeList', {}, function (result) {
        if(result != null){
            if(result.data != null){
                var type_html = "<option value=''>全部</option>";
                $.each(result.data, function (index, item) {
                    type_html += "<option value='" + item + "'>" + item + "</option>";
                });
                $("#openType").empty().append(type_html);
                form.render('select');
            }
        }
    });

    $.get('/zxkDevice/listAll', {}, function (result) {
        if(result != null){
            if(result.data != null){
                var device_html = "<option value=''>全部</option>";
                $.each(result.data, function (index, item) {
                    device_html += "<option value='" + item.serialNum + "'>" + item.deviceName + "</option>";
                });
                $("#openDevice").empty().append(device_html);
                form.render('select');
            }
        }
    });

    table.render({
        elem: '#acsRecordTable'
        ,url:'/zxkRecord/list'
        ,title: '门禁记录'
        ,where: {type:'',name:''}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'communityName', title:'小区',align:'center'}
            ,{field:'serialNum', title:'设备序列号',align:'center'}
            ,{field:'deviceName', title:'设备别名',align:'center'}
            ,{field:'openUser', title:'开门用户',align:'center'}
            ,{field:'phone', title:'电话号码',align:'center'}
            ,{field:'bodyTemperature', title:'体感温度',align:'center'}
            ,{field:'openTime', title:'开门时间',  sort: true,templet:
                    function(data){
                        return formatTime(data.openTime);
                }}
            ,{field:'openType', title:'开门方式',align:'center'}
            ,{field:'capturePic', title:'抓拍图片',align:'center',templet:function (d) {
                    if (d.capturePic) {
                        return '<img onclick="magnifyPic(this)" style="cursor: pointer" width="30" height="25" src="' + d.capturePic + '"/>';
                    }
                    return "";
                }}
        ]]
        ,page: true
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    form.on('submit(query_record)',function () {
        var name = $.trim($("#name").val());
        var startDate= $("#openBeginDate").val();
        var endDate= $("#openEndDate").val();
        var openType= $("#openType").val();
        var serialNum= $("#openDevice").val();
        table.reload('acsRecordTable',{
            where: {name:name,startDate:startDate,endDate:endDate,openType:openType,serialNum:serialNum}
            ,page:  {
                curr:1
            }
        });
        return false;
    });

    $("#syncDevice").on('click', function (){
        layer.confirm('同步数据，是否继续？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {
                shade: [0.4, '#000'],
                content: '同步中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '40px',
                        'width': '200px'
                    });
                }
            });
            $.post('/zxkDevice/syncData',{},function (res) {
                if(res.status === 200){
                    layer.msg(res.msg,{
                        time:500,
                        end:function () {
                            reload();
                        }
                    })
                }else{
                    layer.msg(res.msg);
                }
                layer.close(index);
            });
        })
    });

});

function parseStatus(status) {
    if(status){
        switch (status) {
            case 0 : return "离线";
            case 1 : return "在线";
            default: return "未知";
        }
    }
    return "未知";
}
function reload(){
    location.reload();
}

function magnifyPic(ele) {
    var width = $(ele).width();
    var height = $(ele).height();
    var scaleWH = width/height;
    var bigH = 450;
    var bigW = scaleWH*bigH;
    if(bigW>600){
        bigW = 600;
        bigH = bigW/scaleWH;
    }
    // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='"+bigW+"' height='"+bigH+"' src=" + $(ele).attr("src") + " />"
    });
}